from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QPushButton, QVBoxLayout, QStackedWidget, QAction


def create_page(text, color):
    page = QWidget()
    layout = QVBoxLayout()
    label = QLabel(text)
    label.setStyleSheet(f"font-size: 20px; color: {color};")
    layout.addWidget(label)
    page.setLayout(layout)
    return page


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.page2 = None
        self.page1 = None
        self.stacked_widget = None
        self.page_home = None
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle("主界面")
        self.setGeometry(400, 400, 600, 400)

        # 创建堆叠窗口
        self.stacked_widget = QStackedWidget()
        self.setCentralWidget(self.stacked_widget)

        # 添加页面
        self.page_home = create_page("欢迎来到主页!", "green")
        self.page1 = create_page("这是功能页面1", "lightblue")
        self.page2 = create_page("这是功能页面2", "pink")

        self.stacked_widget.addWidget(self.page_home)
        self.stacked_widget.addWidget(self.page1)
        self.stacked_widget.addWidget(self.page2)

        # 创建菜单栏
        menubar = self.menuBar()
        menu = menubar.addMenu("功能")

        home_action = QAction("主页", self)
        home_action.triggered.connect(lambda: self.switch_page(0))
        menu.addAction(home_action)

        action1 = QAction("功能1", self)
        action1.triggered.connect(lambda: self.switch_page(1))
        menu.addAction(action1)

        action2 = QAction("功能2", self)
        action2.triggered.connect(lambda: self.switch_page(2))
        menu.addAction(action2)

    def switch_page(self, index):
        self.stacked_widget.setCurrentIndex(index)